//___________________________________________增加航节的封装字符串函数——————————————————————————-
function getAddFlightNode(){
	var No = $(".nodeMessDetail").length+1
	var str = "";
	str+="<div class=nodeMessDetail>"+
			"<div class=floor>"+No+"</div>"+
			"<div class=floor>"+
				"<p class=midTop>出发<input class='dep' type=text placeholder=机场 /><span style='color:red;font-size: 16px;'>*</span>"+
					" <input class='terminal depTerminal' type='text' placeholder=航站楼 />"+
					" 时间<input class='depTime' type='text'/><span style='color:red;font-size: 16px;margin-right:20px;'>*</span>"+
					" 日期变更<input class='depDc' type='text' /><span style='color:red;font-size: 16px;'>*</span>"+
				"</p>"+
				"<p class='midTop'>到达<input class='arr' type='text' placeholder=机场 /><span style='color:red;font-size: 16px;'>*</span>"+
					" <input class='terminal arrTerminal' type='text' placeholder=航站楼 />"+
					" 时间<input class='arrTime' type='text'/><span style='color:red;font-size: 16px;margin-right:20px;'>*</span>"+
					" 日期变更<input class='arrDc' type='text' /><span style='color:red;font-size: 16px;'>*</span>"+
				"</p>"+
			"</div>"+
			"<div class='floor'><span>承运航班<input class='OC' type='text'/></span></div>"+
		 	"<div class='floor'><span class='delete'>删除</span></div>"+
		 "</div>"
		 
	$(".nodeMess").append(str)
}


/*___________________________________________________________________________________________添加航节*/

$(".add").click(function(){
	getAddFlightNode()
})

/*_________________________________________________________________________删除功能*/
$('.nodeMess').delegate(".delete","click",function(){
	var index = $(this).parent().parent().index()
	if($(".nodeMessDetail").length>1){
		$(".nodeMessDetail").eq(index).remove()
	}	
})
/*_________________________________________________________________________自动计算*/
document.onkeyup = function(){
	var total = ($(".fseat").val()-0)+($(".cseat").val()-0)+($(".yseat").val()-0)
	$(".totalseat").val(total)
}
/*-________________________________________________________________________确认按钮————————*/
$(".sureButton").click(function(){
	var message = {};		
	message.F = $(".fseat").val();
	message.C = $(".cseat").val();
	message.Y = $(".yseat").val();	
	message.aircrftName = $(".aircraft").val();
	message.flightLegs = [];
	var length = $(".nodeMessDetail").length;
	for(var i=0;i<length;i++){
		var json={};
		json.depAirport = $(".dep").eq(i).val()
		json.arrAirport = $(".arr").eq(i).val()
		json.depTerminal = $(".depTerminal").eq(i).val()
		json.arrTerminal = $(".arrTerminal").eq(i).val()
		json.depTime = $(".depTime").eq(i).val()
		json.depDc = $(".depDc").eq(i).val()
		json.arrTime = $(".arrTime").eq(i).val()
		json.arrDc = $(".arrDc").eq(i).val()
		json.carrCd = $(".OC").eq(i).val()
		json.id = signId
		message.flightLegs.push(json)
	}
	
	$.ajax({
		type:"post",
		url:"../flight/"+postId+"/update?temp="+Math.random(),
		contentType:'application/json;charset=utf-8',
		data:JSON.stringify(message),
		async:true,
		success:function(data){
			data = JSON.parse(data)
			if(data.status=="1001"){
				layer.msg('航班信息修改成功', {icon: 1,time:1000},function(){
					var index = parent.layer.getFrameIndex(window.name);//获取弹窗索引
					parent.layer.close(index);//关闭弹窗
				});			
			}else{
				layer.msg('航班信息修改失败,'+data.message, {icon: 2,time:1000});			
			}
			
			
			
		}
	});
	
})
//打开页面数据渲染——————————————————————————————————————————————————————————————————————————
	var postId = getHref().postId
	$.ajax({
		type:"get",
		url:"../flight/"+postId+"?&temp="+Math.random(),
		success:function(data){
			data = JSON.parse(data)	
			//console.log(data)
			$(".airlineCode").val(data.data.airlineCode)
			$(".pm").val(data.data.createUserName)
			if(data.data.status=="1"){	
				data.data.status="执行中";
			}	
			if(data.data.status=="0"){				
				data.data.status="未开始"
			}
			if(data.data.status=="D"){				
				data.data.status="已完成"				
			}
			if(data.data.status=="2"){				
				data.data.status="已取消"				
			}
			$(".status").val(data.data.status)
			$(".flightDate").val(data.data.firstLegDepDate)
			for(var i=0;i<data.data.flightLegVos.length;i++){
				getAddFlightNode()
				$(".dep").eq(i).val(data.data.flightLegVos[i].depAirport)
				$(".arr").eq(i).val(data.data.flightLegVos[i].arrAirport)
				$(".depTerminal").eq(i).val(data.data.flightLegVos[i].depTerminal)
				$(".arrTerminal").eq(i).val(data.data.flightLegVos[i].arrTerminal)
				$(".depTime").eq(i).val(data.data.flightLegVos[i].depTime)
				$(".arrTime").eq(i).val(data.data.flightLegVos[i].arrTime)
				$(".depDc").eq(i).val(data.data.flightLegVos[i].depDc)
				$(".arrDc").eq(i).val(data.data.flightLegVos[i].arrDc)
				$(".OC").eq(i).val(data.data.flightLegVos[i].carrCd)	
			}			
		}
	})
//-------------------------------------渲染中间部分的数据———————————————————————————————————
	var signId = getHref().signId
	$.ajax({
		type:"get",
		url:"../flight/leg/"+signId+"?&temp="+Math.random(),
		success:function(data){
			data = JSON.parse(data)			
			$(".aircraft").val(data.data.aircrftName)
			for(var i=0;i<data.data.invLegCabinVos.length;i++){				
				if(data.data.invLegCabinVos[i].cabin=="F"){					
					$(".fseat").val(data.data.invLegCabinVos[i].capacity)
				}
				if(data.data.invLegCabinVos[i].cabin=="C"){			
					$(".cseat").val(data.data.invLegCabinVos[i].capacity)
				}
				if(data.data.invLegCabinVos[i].cabin=="Y"){			
					$(".yseat").val(data.data.invLegCabinVos[i].capacity)
				}
				/*if(data.data.invLegCabinVos[i].cabin=="W"){				
					$(".wseat").val(data.data.invLegCabinVos[i].capacity)
				}*/
			}
			var totalseat = ($(".fseat").val()-0)+($(".cseat").val()-0)+($(".yseat").val()-0)								
			$(".totalseat").val(totalseat)
		}
	})






/*——————————————————————————————————————————————获取页面传参的值————————————————————————————*/
function getHref(){
    var s=decodeURI(window.location.search.substr(1));
    var arr1= s.split("&");
    var obj={};
    $(arr1).each(function(i){
        var arr=this.split("=");
        obj[arr[0]]=arr[1];
    })
    return obj;
}
